package com.campus.controller;

import com.campus.model.Student;
import com.campus.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @desc:
 * @author: Tournesol234
 * @date: 2024/11/22 14.41
 */
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/create")
    public ResponseEntity<String> createStudent(@RequestBody Student student) {
        int result = studentService.createStudent(student);
        return result > 0 ? ResponseEntity.ok("Student created successfully") : ResponseEntity.status(500).body("Failed to create student");
    }

    @PostMapping("/create/batch")
    public ResponseEntity<String> batchCreateStudents(@RequestBody List<Student> students) {
        int result = studentService.batchCreateStudents(students);
        return result > 0 ? ResponseEntity.ok("Students created successfully") : ResponseEntity.status(500).body("Failed to create students");
    }

    @PutMapping("/{id}/update")
    public ResponseEntity<String> updateStudent(@PathVariable int id, @RequestBody Student student) {
        int result = studentService.updateStudent(id, student);
        return result > 0 ? ResponseEntity.ok("Student updated successfully") : ResponseEntity.status(500).body("Failed to update student");
    }

    @DeleteMapping("/{id}/delete")
    public ResponseEntity<String> deleteStudent(@PathVariable int id) {
        int result = studentService.deleteStudent(id);
        return result > 0 ? ResponseEntity.ok("Student deleted successfully") : ResponseEntity.status(500).body("Failed to delete student");
    }

    @GetMapping("/{id}")
    public ResponseEntity<Student> getStudentById(@PathVariable int id) {
        Student student = studentService.getStudentById(id);
        return student != null ? ResponseEntity.ok(student) : ResponseEntity.status(404).body(null);
    }

    @GetMapping("/all")
    public ResponseEntity<List<Student>> getAllStudents() {
        List<Student> students = studentService.getAllStudents();
        return students != null ? ResponseEntity.ok(students) : ResponseEntity.status(404).body(null);
    }

    @PutMapping("/approve/{id}")
    public ResponseEntity<String> approveStudent(@PathVariable int id) {
        int result = studentService.approveStudent(id);
        return result > 0 ? ResponseEntity.ok("Student approved successfully") : ResponseEntity.status(500).body("Failed to approve student");
    }

    @PutMapping("/approve/batch")
    public ResponseEntity<String> batchApproveStudents(@RequestBody List<Integer> studentIds) {
        int result = studentService.batchApproveStudents(studentIds);
        return result > 0 ? ResponseEntity.ok("Students approved successfully") : ResponseEntity.status(500).body("Failed to approve students");
    }
}
